/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.menu;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

import fr.upvm.lem3.vm3d.ui.swing.frontend.MainGUI;

/**
 *  Toolbar for the main controls action like load et save data
 * @author Nougouapi
 *
 */
public class MainControlsToolBar extends JToolBar implements ActionListener{

	 /**
	 * 
	 */
	private static final long serialVersionUID = -2044873285052362799L;
	private JFrame parentFrame;
    private MainControlsToolBarController toolBarController;
    private JButton btn_Open; // use to open the panel which it used to load the file
   
    private MyMouseAdpter mouseAdapter;
    /*
     * use to construct a toolbar object
     */
	public MainControlsToolBar( JFrame frame){
		 super(frame.getTitle()+" - Main Controls");
		 this.setToolTipText("Main Controls Toolbar");
		 this.setFloatable(false);
		 this.mouseAdapter = new MyMouseAdpter();
		 this.setParentFrame(frame);
		 this.setToolBarController(new MainControlsToolBarController(this));
		 this.setBorder(BorderFactory.createEmptyBorder());
		 initComponents();
	 }
   
	 /**
	  * handle the actions with happen on click on these 2 buttons.
	  */
	public void actionPerformed(ActionEvent e) {
		//TODO: to complete
		String cmd= e.getActionCommand();	
		if(OPEN.equals(cmd)){
		   ((MainGUI)getParentFrame()).displayDataLoader(); //get the dataLoader window visible
		}
	}
	/**
	 * initialize the menuBar
	 */
	private void initComponents(){
		this.setRollover(true);
		this.addSeparator(new Dimension(5,5));	
		this.add(getBtn_Open());
		this.addSeparator(new Dimension(10,10));	
	}

	/**
	 * @param toolBarController the toolBarController to set
	 */
	public void setToolBarController(MainControlsToolBarController toolBarController) {
		this.toolBarController = toolBarController;
	}

	/**
	 * @return the toolBarController
	 */
	public MainControlsToolBarController getToolBarController() {
		return toolBarController;
	}
	 
	static final private String TOOLTIP_OPEN="Load the data to visualize";
	static final private String OPEN_LABEL = "Load";
	 static final private String OPEN="Open";
	/**
	 * @return the btn_Open
	 */
	public JButton getBtn_Open() {
		if(btn_Open== null){
			btn_Open= new JButton();
			btn_Open.setFont(new Font("Dialog", Font.TRUETYPE_FONT, 15));
			btn_Open.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
			btn_Open.setAlignmentX(Component.CENTER_ALIGNMENT);
			btn_Open.setFocusable(false);
			btn_Open.setText(OPEN_LABEL);
			btn_Open.setToolTipText(TOOLTIP_OPEN);
			btn_Open.setOpaque(false);
			btn_Open.addActionListener(this);
			btn_Open.setActionCommand(OPEN);
			btn_Open.setMnemonic(KeyEvent.VK_L);
			btn_Open.addMouseListener( mouseAdapter);
			
		}
		return btn_Open;
	}
	/**
	 * @param btn_Open the btn_Open to set
	 */
	public void setBtn_Open(JButton btn_Open) {
		this.btn_Open = btn_Open;
	}
	/**
	 * @param parentFrame the parentFrame to set
	 */
	public void setParentFrame(JFrame parentFrame) {
		this.parentFrame = parentFrame;
	}

	/**
	 * @return the parentFrame
	 */
	public JFrame getParentFrame() {
		return parentFrame;
	}
	/**
	 * Internal adapter class
	 *******************************************************************
	 */
	class MyMouseAdpter extends MouseAdapter{
		
		public MyMouseAdpter() {}
		
		@Override
	    public void mouseEntered(MouseEvent e) {
			((JButton)e.getSource()).setBorder(BorderFactory.createLineBorder(Color.lightGray, 1, true));
		}

	    @Override
	    public void mouseExited(MouseEvent e) {
	    	((JButton)e.getSource()).setBorder(BorderFactory. createEmptyBorder(1,1,1,1));
	    }
	}
}
